//hash function - 1


//reading username and passwords from the file
#include<stdio.h>
#include<string.h>
#include<malloc.h>
int m=23;

int ascii_check(char p[])
{
	
	int str_len,i;
	
	int asc;
	int sum=0;
	str_len=strlen(p);
	for(i=0;i<=str_len-1;i++)
	{
	                  asc=p[i];
	                  sum=sum+asc;
	}
	printf("the sum of ascii values of the string is %d",sum);
	printf("\n");
	return sum;
}

int hash_func_1(int k)
{
	int hash1;
	hash1=k%m;
	return hash1;
}

struct node{
	
	char user[20];
	char pass[20];
	struct node *next;
};
/*9868340240
struct node *read_value()
{
        int a,num,mu;
        struct node *first=NULL;
       // printf("how manuy values you want to enter:");
       // scanf("%d",&len);
    //printf("enter the values(from LSB to MSB):");
    //for(mu=0;mu<=len-1;mu++)
    //{
      //                      scanf("%d",&a);

         first=add_to_list(first,a);
    
	//}
    return first;
    
}

struct node *insert_begin(struct node *first,char p[], char q[])
{
	struct node *ins;
	ins=malloc(sizeof(struct node));
	
	strcpy(ins->user,p);
	strcpy(ins->pass,q);
	first=ins;
	
}
*/
struct node *add_to_list(struct node *first,char p[], char q[]) 
{
        struct node *new_node;
        new_node=malloc(sizeof(struct node));
        strcpy(new_node->user,p);
        strcpy(new_node->pass,q);
	new_node->next=first;
        return new_node;
}
	

/*int hash_implement()
{
	struct node (*a)[23];
			

*/
struct node *append_value(struct node *first,int n[],int m[])
{
       	struct node *app;
	app=malloc(sizeof(struct node));
	first->next=app;
	strcpy(app->user,n);
	strcpy(app->pass,m);
	app->next=NULL;

	return first;
}


char * index_list(struct node *first, char n[])
{
        char *ind;
        for(;first!=NULL;first=first->next)
        {
                //ind=ind+1;
                if(strcmp(first->user,n)==0)
                {
			strcpy(ind,first->pass);
                        break;
                }
        }
	return ind;
        //printf("the index of the number %d is %d\n",n,ind);
}


int main()
{
	FILE *fp;
	char s[80];
	char p[15];
	char q[15];
	int s1,hash1;
	fp=fopen("userpass_2.txt","r");
	if(fp==NULL)
	{
		puts("cannot open file");
		exit(0);
	}
	char ch;
	//int i=0;
	int m,l;
	int j=0;
	//int k=0;
	//do{
	
	//
	int ac=0;
	
	struct node *a[23];
	a[0]=NULL;
//printf("%c",ch);
	while(ac<=2)
{
	int count=0,k=0,i=0;
	ch=fgetc(fp);
	//fgets(s,79,fp);	
	while(ch!='\n')
	{
		//printf("%c",ch);
		s[i]=ch;
		i++;
		count++;
		ch=fgetc(fp);
	}
	//printf("\n%d",count);
	s[i]='\0';
	//printf("\n");
	//printf("%d\n",k);
	
	while(s[k]!=' ')
	{
		p[k]=s[k];
		printf("%c",p[k]);
		k++;
		//count++;
	}
	p[k]='\0';
	int temp[80];
	
	printf("\n");
	for(m=0,l=k;l<=count-1;l++,m++)
	{
		q[m]=s[l];
		printf("%c",q[m]);
	}
	q[++m]='\0';
	printf("\n");
	
	
	printf("\n");
	//}while(ch!='EOF');


	//   hash function
	
	s1=ascii_check(p);
	hash1=hash_func_1(s1);
	
	printf("the home location of %s is %d",p,hash1);
	printf("\n");
	printf("%d\n",hash1);
	//a=NULL;
	//if(a[hash1]==NULL)
	//{
		a[hash1]=add_to_list(a[hash1],p,q);
	//}
	//else if(a[hash1]!=NULL)
	//{
		//a[hash1]=append_value(a[hash1],p,q);	
//	}
	//printf("%s\n",a[ac]->user);
	
	ac++;

}
	/*while(s[j]!='\0')
	{
		printf("%c",s[j]);
		j++;
	}*/

	/*struct node *a;
	a=NULL;
	
	a=add_to_list(a,p,q);
	printf("%s",a->user);
	printf("\n");*/
	//printf("%s",a->user);

	printf("\n%s\n",a[20]->user);




	int h2,hash2;
	char user_name[40];
	printf("enter the username :");
	gets(user_name);
	h2=ascii_check(user_name);
	//printf("hello");
	hash2=h2%m;
	//hash2=hash_func_1(h2);
	
	char *pass_1;
	char *pass_2;

	pass_2=index_list(a[hash2],user_name);
	printf("enter the password:");
	printf("hello");
	gets(pass_1);
	
	if(strcmp(pass_2,pass_1)==0)
	{
		printf("you have successfully logged in");
	}
	else
	{
		printf("either the usernmae or the password enetered by you is wrong");
	}
}

